package com.gg.user.gguser.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.gg.CharacterEnums;
import com.gg.cookice.cookice;
import com.gg.encryption.AES;
import com.gg.user.gguser.dao.GgBindingDaoMapper;
import com.gg.user.gguser.dao.GgInvitationCodeDaoMapper;
import com.gg.user.gguser.dao.GgUserDao;
import com.gg.user.gguser.dao.GgUserDaoMapper;
import com.gg.user.gguser.dataobject.GgInvitationCodeDataobject;
import com.gg.user.gguser.dataobject.GgUserDataobject;
import com.gg.user.gguser.service.GgInvitationCodeServe;
import com.gg.user.gguser.service.GgUserService;
import com.gg.util.*;
import com.gg.vo.return_format;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GgUserServiceImpl implements GgUserService {

    @Autowired
    private GgUserDao ggUserDao;

    @Autowired
    private GgUserDaoMapper ggUserDaoMapper;

    @Autowired
    private GgInvitationCodeDaoMapper ggInvitationCodeDaoMapper;

    @Autowired
    private GgBindingDaoMapper ggBindingDaoMapper;

    @Override
    // 后台获取用户数据完整的
    public Object getUser(String ggid, HttpServletResponse response, HttpServletRequest request) {
        return ggUserDao.getUser(ggid);
    }

    @Override
    public Object getUserQ(String ggid, HttpServletResponse response, HttpServletRequest request) {
        return ggUserDao.getUserQ(ggid);
    }

    @Override
    public Object getUserAll(HttpServletResponse response,Integer page, Integer pageCount) {
        if(page != null && page > 0 && pageCount != null && pageCount > 0 ){
            try {
                // 参数误传时候提示不要瞎弄
                Integer getUserSelectCountNumber = ggUserDaoMapper.getUserSelectCount();

                List getUserAllList =  ggUserDao.getUserAll((page-1)*pageCount, pageCount);
                //设置cookice
                cookice.addCookice(response,"b","b");
                return return_format.return_format_All(0,return_format.return_format_data_map(page, (int) Math.ceil(getUserSelectCountNumber/pageCount),pageCount,getUserSelectCountNumber,getUserAllList),"");
            }catch(Exception e){
                return return_format.return_format_error(100003,"服务有问题了");
            }
        }else {
            return return_format.return_format_error(100002,"用户自有数据是允许爬虫，但是参数不要瞎传！");
        }

    }

    /**
     *
     * @param userName  账号
     * @param name 用户名
     * @param passWord  密码
     * @param ZPassWord 验证是否正确密码
     * @param phone 手机号
     * @param mailbox 邮箱
     * @return
     */
    // 用户名可以重复但是账号不能重复
    @Override
    public Object addUserRegister(HttpServletResponse response,String userName,String name,String passWord,String ZPassWord,String phone,String mailbox,String invitation) throws Exception {

        // 验证用户名  不能有中文也不能小于5位 大于25位
        boolean CharacterUserName = CharacterCommon.isContainChineseYes(userName);
        if(!CharacterUserName){
            return return_format.return_format_error(100004,"用户名不符合规格，请重新输入！");
        }

        // 验证手机号
        boolean yesPhone= PhoneCommon.isPhone(phone);
        if(!yesPhone){
            return return_format.return_format_error(100004,"请输入正确手机号！");
        }
        // 密码不能包含中文 不能低于6个字符 大于25个
        boolean PassWordYanZheng = CharacterCommon.StringYanZhengPassWord(passWord);

        if(!passWord.equals(ZPassWord) | !PassWordYanZheng){
            return return_format.return_format_error(100004,"密码不符合规则,请重新输入密码！");
        }

        // 判断邮箱是不是正确
        if (!mailbox.isEmpty()){
            if(!mailboxCommon.isEmail(mailbox)){
                return return_format.return_format_error(100004,"邮箱不符合规则，请重新输入邮箱！");
            }

        }

        // 判断用户名是否存在
        Integer userCount = ggUserDaoMapper.getUserNameSelectCount(userName);
        if(userCount != 0){
            return return_format.return_format_error(100004,"用户名已存在请重新输入！");
        }
        String keyUsername = KeyUtilCommon.genUniqueKey() + userName;




        // 密码加密
        String passwordjiami = AES.AESEncode(keyUsername, passWord);
        try {
            // 写入cookice 和 数据
            Object userInformation = null;
            // 判断验证码是不是空如果不是空的说明邀请来的，直接进行主体绑定
            if(!invitation.isEmpty()){
                try {
                    ggUserDaoMapper.installName(userName,passwordjiami,name,mailbox,phone,keyUsername,4);
                    userInformation = ggUserDaoMapper.getUserNameSelectQ(userName);// 获取用户
                    GgInvitationCodeDataobject userGetInvitation = ggInvitationCodeDaoMapper.getInvitationUser(invitation);
                    //绑定上下级关系
                    ggBindingDaoMapper.bindingUser(userGetInvitation.getGgIcEstablishUser(),((GgUserDataobject) userInformation).getGgId());
                }catch (Exception e){
                    return return_format.return_format_error(10002,"服务器报错！");
                }
            }else {
                ggUserDaoMapper.installName(userName,passwordjiami,name,mailbox,phone,keyUsername,5);
                userInformation = ggUserDaoMapper.getUserNameSelectQ(userName);// 获取用户
            }

            //cookice写入数据
            cookice.addCookice(response,"gg", URLEncoder.encode((String) BaseCommon.codebase(JSONObject.toJSONString(userInformation)), "utf-8"));
            return return_format.return_format_All(0,"","");
        }catch (Exception e){
            return return_format.return_format_error(100003,"服务有问题了");
        }
    }

    @Override
    public Object getUserNameSelectCount(String userName) {
        if(userName != null){
            Integer userCount = ggUserDaoMapper.getUserNameSelectCount(userName);
            if(userCount == 0){
                return return_format.return_format_All(0,"","");
            }else {
                return return_format.return_format_error(100003,"用户名已存在请重新输入！");
            }
        }else {
            return return_format.return_format_error(100002,"用户自有数据是允许爬虫，但是参数不要瞎传！");
        }
    }


}
